package cn.elwy.kit.file.dialog;

import java.io.File;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;

import cn.elwy.kit.file.service.BackupRecoveryFile;

public class BackupRecoveryFileDialog {

    protected Shell shell;
    private Text srcText;
    private Text destText;
    private Button srcButton;
    private Button destButton;
    private Text recoverText;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            BackupRecoveryFileDialog window = new BackupRecoveryFileDialog();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        GridLayout gl_shell = new GridLayout(1, false);
        gl_shell.marginWidth = 10;
        gl_shell.marginHeight = 10;
        shell.setLayout(gl_shell);

        TabFolder tabFolder = new TabFolder(shell, SWT.NONE);
        tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

        TabItem backTabItem = new TabItem(tabFolder, SWT.NONE);
        backTabItem.setText("备份");

        Composite backComposite = new Composite(tabFolder, SWT.NONE);
        backTabItem.setControl(backComposite);
        backComposite.setLayout(new GridLayout(3, false));

        Label lblNewLabel = new Label(backComposite, SWT.NONE);
        lblNewLabel.setSize(48, 17);
        lblNewLabel.setText("源目录：");

        srcText = new Text(backComposite, SWT.BORDER);
        srcText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
        srcText.setSize(294, 23);

        srcButton = new Button(backComposite, SWT.NONE);
        srcButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String filePath = openDirectoryDialog(shell);
                srcText.setText(filePath);
            }
        });
        srcButton.setText("选择目录");

        Label lblNewLabel_1 = new Label(backComposite, SWT.NONE);
        lblNewLabel_1.setText("备份目录：");

        destText = new Text(backComposite, SWT.BORDER);
        destText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        destButton = new Button(backComposite, SWT.NONE);
        destButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                String filePath = openDirectoryDialog(shell);
                destText.setText(filePath);
            }
        });
        destButton.setText("选择目录");
        new Label(backComposite, SWT.NONE);

        Button okButton = new Button(backComposite, SWT.NONE);
        okButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
        okButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent event) {
                String srcPath = srcText.getText();
                String destPath = destText.getText();
                try {
                    File srcFile = new File(srcPath);
                    if (!srcFile.exists()) {
                        throw new RuntimeException(srcPath + "不存在！");
                    }
                    File destFile = new File(destPath);
                    if (destFile.isFile()) {
                        throw new RuntimeException(destPath + "不可以是文件！");
                    }

                    BackupRecoveryFile.copyFiles(srcFile, destFile, "");
                    MessageDialog.openInformation(shell, "信息", "文件输出成功！");
                    return;
                } catch (Exception e) {
                    MessageDialog.openError(shell, "错误", e.getMessage());
                    return;
                }
            }
        });
        okButton.setText("确认");
        new Label(backComposite, SWT.NONE);

        TabItem recoverTabItem = new TabItem(tabFolder, SWT.NONE);
        recoverTabItem.setText("恢复");

        Composite composite = new Composite(tabFolder, SWT.NONE);
        recoverTabItem.setControl(composite);
        composite.setLayout(new GridLayout(3, false));

        Label label = new Label(composite, SWT.NONE);
        label.setText("源目录：");

        recoverText = new Text(composite, SWT.BORDER);
        recoverText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

        Button button = new Button(composite, SWT.NONE);
        button.setSize(60, 27);
        button.setText("选择目录");
        new Label(composite, SWT.NONE);

        Button button_1 = new Button(composite, SWT.NONE);
        button_1.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent event) {
                String srcPath = recoverText.getText();
                try {
                    File srcFile = new File(srcPath);
                    if (!srcFile.exists()) {
                        throw new RuntimeException(srcPath + "不存在！");
                    }

                    BackupRecoveryFile.renameTo(srcFile);
                    MessageDialog.openInformation(shell, "错误", "文件输出成功！");
                    return;
                } catch (Exception e) {
                    MessageDialog.openError(shell, "错误", e.getMessage());
                    return;
                }

            }
        });
        button_1.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1));
        button_1.setText("确认");
        button_1.setBounds(0, 0, 36, 27);
        new Label(composite, SWT.NONE);

    }

    protected String openDirectoryDialog(Shell parent) {
        // 新建文件对话框，并设置为打开的方式
        DirectoryDialog dialog = new DirectoryDialog(parent);
        // 设置文件对话框的标题
        dialog.setText("选择目录");
        // 设置初始路径
        dialog.setFilterPath("SystemDrive");
        // 设置对话框提示文本信息
        dialog.setMessage("请选择相应的文件夹");
        // 打开文件对话框，返回选中文件的绝对路径
        String selected = dialog.open();
        return selected == null ? "" : selected;
    }

    protected String openFileDialog(Shell parent) {
        // 新建文件对话框，并设置为打开的方式
        FileDialog dialog = new FileDialog(parent, SWT.OPEN);
        // 设置文件对话框的标题
        dialog.setText("文件选择");
        // 设置初始路径
        dialog.setFilterPath("SystemRoot");
        // 打开文件对话框，返回选中文件的绝对路径
        // 设置对话框提示文本信息
        String selected = dialog.open();
        return selected == null ? "" : selected;
    }

    public static String getExtName(String fileName) {
        int index = fileName.lastIndexOf(".");
        return index != -1 ? fileName.substring(index + 1) : "";
    }
}
